<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:classify:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="classifyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="分类名称" align="center" prop="classifyName" />
      <el-table-column label="级别" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <p v-show="scope.row.level===1">
            一级
         </p>
          <p v-show="scope.row.level===2">
            二级
          </p>
        </template>
      </el-table-column>
      <el-table-column label="商品数量" align="center" prop="produceCount"  />
      <el-table-column label="数量单位" align="center" prop="countUnit" />
      <el-table-column label="是否显示" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.reveal"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            @change="negationReveal(scope.row.id)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="导航栏" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.navigation"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            @change="negationManufacturer(scope.row.id)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="classifyRank" />
      <el-table-column label="设置" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showNext(scope.row.id)"
            :disabled="scope.row.parentId !== 0"
            v-hasPermi="['produce:classify:showNext']"
          >查看下级</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['produce:brand:remove']"
          >转移商品</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:brand:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['produce:brand:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="分类名称" >
          <el-input v-model="form.classifyName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="上级分类" >
          <el-select v-model="form.parentId"  clearable>

            <el-option
              :value="0"
              label="无上级分类">
            </el-option>

            <el-option
              v-for="c in classify"
              :key="c.id"
              :label="c.classifyName"
              :value="c.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数量单位" >
          <el-input v-model="form.countUnit" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="排序" >
          <el-input v-model="form.classifyRank" placeholder="请输入参数名称" />
        </el-form-item>

        <el-form-item label="是否显示" >
          <el-radio-group v-model="form.reveal">
            <el-radio :label="true" >是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="导航栏" >
          <el-radio-group v-model="form.navigation">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分类图标" >
          <el-upload
            class="upload-demo"
            action="http://localhost/dev-api/produce/brand/upload"
            :on-success="handleSuccess"
            >
            <el-image
              style="width: 100px; height: 100px"
              v-show="form.classifyIcon != null"
              :src="form.classifyIcon"
            ></el-image>
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="筛选属性" >
          <div class="block">
            <el-cascader
              v-model="form.typeWithProperty"
              :options="properties"
              ></el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="关键词" >
          <el-input v-model="form.keyword" placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item label="描述" >
          <el-input v-model="form.description" placeholder="请输入参数键值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClassify,addClassify, updateClassify,delClassify ,
  getClassify,changeReveal, changeNavigation,getFirstClassify,getTypeTree} from "@/api/produce/classify";

export default {
  name: "Classify",
  data() {
    return {
      classifies:[],

      properties:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      classifyList: [],
      // 一级分类
      classify: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: 0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classifyName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    showNext(id){
      this.queryParams.parentId=id;
      this.getList()
    },

    /**转换是否显示状态*/
    negationReveal(id){
      changeReveal(id);
      this.getList()
    },

    /**转换是否是供应商状态*/
    negationManufacturer(id){
      changeNavigation(id);
      this.getList()
    },

    /**上传Logo*/
    handleSuccess(response) {
      this.form.classifyIcon=response.data.url;
    },


    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listClassify(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.classifyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      getFirstClassify().then(response => {
        this.classify = response.data;
      });

      getTypeTree().then(response => {
        this.properties = response.data;
      });

      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getTypeTree().then(response => {
        this.properties = response.data;
      });
      getFirstClassify().then(response => {
        this.classify = response.data;
      });
      const classifyId = row.id || this.ids;
      getClassify(classifyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateClassify(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClassify(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const classifyId = row.id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + classifyId + '"的数据项？').then(function() {
          return delClassify(classifyId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</script>
